<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // await 等待，等待后面的表达式结果

        // 通常，await是等待后面的Promise对象的结果
        // await 的作用类似于Promie.then

        // await命令必须在async函数中使用
        

        async function async1(){
            let res1 = await 2+5 ;
            console.log(res1);

            let res2 = await new Promise((resolve,reject)=>{
                setTimeout(()=>{
                    console.log('异步任务完成');
                    resolve(123);
                },1000)
            });

            console.log('成功的结果',res2);

            let res3 = await new Promise((resolve,reject)=>{
                setTimeout(()=>{
                    console.log('异步任务完成2');
                    reject(55);
                },1000)
            }).catch((res)=>{
                console.log('异常'+res);
            })

            console.log('失败的结果为'+res3);


            try{
                var res4 = await new Promise((resolve,reject)=>{
                    setTimeout(()=>{
                        console.log('异步任务完成3');
                        reject(789);
                    },1000)
                })
            }catch(res){
                console.log('异常',res);
                return res;
            }

            console.log(res4);
        }

        let p1 = async1();

        (async function(){
            let res1 = await 1+2;
            let res2 = await 2+3;
            console.log(res1);
            console.log(res2);
        })();
    </script>
</body>
</html>